package com.bjc.base.handler;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.bjc.base.builder.ImageBuilder;
import com.bjc.base.builder.TextBuilder;

import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.api.WxConsts.XmlMsgType;
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;

/**
 * @author Binary Wang(https://github.com/binarywang)
 */
@Component
public class MsgHandler extends AbstractHandler {

	
	@Value("${myserver.url}")
	private String myserverUrl;

	@Override
	public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage, Map<String, Object> context, WxMpService wxMpService,
			WxSessionManager sessionManager) throws WxErrorException {

		if (!wxMessage.getMsgType().equals(XmlMsgType.EVENT)) {
			// 可以把消息添加到数据库
		}

		// TODO 组装回复消息
		// String content = "收到信息内容：" + JSON.toJSONString(wxMessage);
		String content = wxMessage.getContent();
		try {
			// 当用户输入关键词如“你好”，“客服”等，并且有客服在线时，把消息转发给在线客服
			if (StringUtils.startsWithAny(content, "你好")
					&& wxMpService.getKefuService().kfOnlineList().getKfOnlineList().size() > 0) {
				return WxMpXmlOutMessage.TRANSFER_CUSTOMER_SERVICE().fromUser(wxMessage.getToUser())
						.toUser(wxMessage.getFromUser()).build();
			} else if (StringUtils.startsWithAny(content, "借阅地点")) {
				return new TextBuilder().build(MsgConst.jydd, wxMessage, wxMpService);

			} else if (StringUtils.startsWithAny(content, "借阅绘本")) {
				return new TextBuilder().build(MsgConst.jyhb, wxMessage, wxMpService);
				
			} else if (StringUtils.startsWithAny(content, "分享绘本")) {
				return new TextBuilder().build(MsgConst.fxhb, wxMessage, wxMpService);
				
			} else if (StringUtils.startsWithAny(content, "选择绘本")) {
				return new TextBuilder().build(MsgConst.xzhb, wxMessage, wxMpService);
				
			} else if (StringUtils.startsWithAny(content, "免费活动")) {
				return new TextBuilder().build(MsgConst.mfhd, wxMessage, wxMpService);
				
			} else if (StringUtils.startsWithAny(content, "修改")) {
				String scanReadPlaceUrl = wxMpService.oauth2buildAuthorizationUrl(myserverUrl + "/scanReadPlace.html", WxConsts.OAuth2Scope.SNSAPI_USERINFO, null);
				return new TextBuilder().build(scanReadPlaceUrl, wxMessage, wxMpService);
			}else if (StringUtils.startsWithAny(content, "其他")) {
				return new TextBuilder().build(MsgConst.qt, wxMessage, wxMpService);
				
			} else {
				return new TextBuilder().build("已经收到消息：" + content, wxMessage, wxMpService);
			}

		} catch (WxErrorException e) {
			e.printStackTrace();
		}

		// return new TextBuilder().build(content, wxMessage, wxMpService);
		return null;

	}

	public static void main(String[] args) {
		System.out.println(ClassLoader.getSystemResourceAsStream("fenxiang.jpg"));
	}

}
